package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketRegionExample;
import com.cskaoyan.market.db.mapper.MarketRegionMapper;
import com.cskaoyan.market.service.AdminRegionService;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.vo.AdminRegionVO;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/7/25
 * Time: 17:59
 * Description:
 */
public class AdminRegionServiceImpl implements AdminRegionService {
    @Override
    public List<AdminRegionVO> list() {
        /**
         * @param : none
         * @return List<AdminRegionBO>
         * @author Lazarus_Liu
         * @description 封装数据, 返回给Controller
         * @date 2023/7/25 19:30
         */
        // 用于返回数据的list
        ArrayList<AdminRegionVO> list = new ArrayList<>();

        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketRegionMapper mapper = session.getMapper(MarketRegionMapper.class);

        // 1. 筛选出省级行政区
        MarketRegionExample provinceMarketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria provinceCriteria = provinceMarketRegionExample.createCriteria();
        provinceCriteria.andTypeEqualTo((byte) 1);
        List<MarketRegion> provinceRegions = mapper.selectByExample(provinceMarketRegionExample);
        // 2. 筛选出市级行政区
        MarketRegionExample cityMarketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria cityCrietria = cityMarketRegionExample.createCriteria();
        cityCrietria.andTypeEqualTo((byte) 2);
        List<MarketRegion> cityRegions = mapper.selectByExample(cityMarketRegionExample);
        // 3. 筛选出县级行政区
        MarketRegionExample areaMarketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria areaCriteria = areaMarketRegionExample.createCriteria();
        areaCriteria.andTypeEqualTo((byte) 3);
        List<MarketRegion> areaRegions = mapper.selectByExample(areaMarketRegionExample);


        // 4. 对筛选出来的数据进行封装 id name type code
        for (MarketRegion provinceRegion : provinceRegions) {
            AdminRegionVO provinceVO = new AdminRegionVO();
            provinceVO.setId(provinceRegion.getId());
            provinceVO.setName(provinceRegion.getName());
            provinceVO.setType(provinceRegion.getType());
            provinceVO.setCode(provinceRegion.getCode());
            // childrenList存放children
            ArrayList<AdminRegionVO> children1 = new ArrayList<>();
            for (MarketRegion cityRegion : cityRegions) {
                // province.id==city.pid
                if(Objects.equals(provinceRegion.getId(), cityRegion.getPid())){
                    AdminRegionVO cityVO = new AdminRegionVO();
                    cityVO.setId(cityRegion.getId());
                    cityVO.setName(cityRegion.getName());
                    cityVO.setType(cityRegion.getType());
                    cityVO.setCode(cityRegion.getCode());
                    // childrenList存放children
                    ArrayList<AdminRegionVO> children2 = new ArrayList<>();

                    for (MarketRegion areaRegion : areaRegions) {
                        if(cityRegion.getId().equals(areaRegion.getPid())){
                            AdminRegionVO areaBO = new AdminRegionVO();
                            areaBO.setId(areaRegion.getId());
                            areaBO.setName(areaRegion.getName());
                            areaBO.setType(areaRegion.getType());
                            areaBO.setCode(areaRegion.getCode());
                            // children为null
                            areaBO.setChildren(null);
                            // 加入上级children
                            children2.add(areaBO);
                        }
                    }
                    // 赋值给children
                    cityVO.setChildren(children2);
                    // 加入上级children
                    children1.add(cityVO);
                }
            }
            // 赋值给children
            provinceVO.setChildren(children1);
            // 加入list
            list.add(provinceVO);
        }
        // 提交事务
        session.commit();
        session.close();
        // 返回给controller
        return list;
    }
}
